package com.mobilekites.gcm;

import java.util.List;
import java.util.Random;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.mobilekites.MobileKitesApp;
import com.mobilekites.R;
import com.mobilekites.util.KitesPrefers.AdOldPrefers;

public class AdsController {
	private static final String TAG = "AdsController";

	/**
	 * Load ads hien thi tren notification
	 */
	private static final int TYPE_NOTIFICATION = 0;
	/**
	 * Ads hien thi trong app
	 */
	private static final int TYPE_INAPP = 1;
	private static final int TYPE_LAUNCH_APP = 2;

	private Context mContext;
	private JSONObject mJsonObject;

	public AdsController(Context context, JSONObject jsonObject) {
		mContext = context;
		mJsonObject = jsonObject;
	}

	public void execute() {
		Context context = mContext;
		JSONObject jsonObject = mJsonObject;
		try {
			int adstype = jsonObject.getInt("adstype");
			String title = jsonObject.getString("title");
			String message = jsonObject.getString("message");
			String url = jsonObject.getString("url");
			String packageName = jsonObject.getString("package");

			if (adstype == TYPE_NOTIFICATION
					&& !isApplicationInstalled(context, packageName)) {
				generateNotification(context, title, message, url);
			} else if (adstype == TYPE_INAPP
					&& !isApplicationInstalled(context, packageName)) {
				AdOldPrefers adOldPrefers = MobileKitesApp.get()
						.getAdOldPrefers();
				adOldPrefers.setIsPromoteApp(true);
				adOldPrefers.savePromoteURL(url);
				adOldPrefers.savePromoteTitle(title);
				adOldPrefers.savePromoteMessage(message);
			} else if (adstype == TYPE_LAUNCH_APP) {
				generateNotification(mContext, title, message);
			}

		} catch (JSONException e) {
			Log.e(TAG, "Execute error parse json");
			e.printStackTrace();
		}
	}

	private void generateNotification(Context context, String title,
			String message, String url) {
		NotificationCompat.Builder builder = new NotificationCompat.Builder(
				context);
		builder.setAutoCancel(true);
		builder.setContentTitle(title);
		builder.setContentText(message);
		builder.setSmallIcon(R.drawable.ic_launcher);
		builder.setDefaults(Notification.DEFAULT_ALL);
		builder.setWhen(System.currentTimeMillis());

		Intent resultIntent = new Intent(Intent.ACTION_VIEW);
		resultIntent.setData(Uri.parse(url));

		resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
				0, resultIntent, 0);

		builder.setContentIntent(resultPendingIntent);
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.notify(new Random().nextInt(100), builder.build());
	}

	private void generateNotification(Context context, String title,
			String message) {
		NotificationCompat.Builder builder = new NotificationCompat.Builder(
				context);
		builder.setAutoCancel(true);
		builder.setContentTitle(title);
		builder.setContentText(message);
		builder.setSmallIcon(R.drawable.ic_launcher);
		builder.setDefaults(Notification.DEFAULT_ALL);
		builder.setWhen(System.currentTimeMillis());

		Intent resultIntent = mContext.getPackageManager()
				.getLaunchIntentForPackage(mContext.getPackageName());

		resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
				0, resultIntent, 0);

		builder.setContentIntent(resultPendingIntent);
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.notify(new Random().nextInt(100), builder.build());
	}

	private boolean isApplicationInstalled(Context context, String packageName) {
		List<ApplicationInfo> list = context.getPackageManager()
				.getInstalledApplications(0);
		for (ApplicationInfo applicationInfo : list) {
			if (packageName.equals(applicationInfo.packageName))
				return true;
		}
		return false;
	}
}
